import os

def main(filename1, filename2, filepath1='base', filepath2='base', resultUrl='./result/'):
    """
    main
    :param filename1:
    :param filename2:
    :param filepath1: base, result or path
    :param filepath2: base, result or path
    :return:
    """
    baseDataUrl = './data/baseData/'
    resultDataUrl = './data/resultData/'

    if (filepath1 == 'base'):
        filepath1 = f'{baseDataUrl}{filename1}'
    elif filepath1 == 'result':
        filepath1 = f'{resultDataUrl}{filename1}'
    else:
        filepath1 = f'{filepath1}{filename1}'

    if (filepath2 == 'base'):
        filepath2 = f'{baseDataUrl}{filename2}'
    elif filepath2 == 'result':
        filepath2 = f'{resultDataUrl}{filename2}'
    else:
        filepath2 = f'{filepath2}{filename2}'

    resultUrl = f'{resultUrl}{filename1} {filename2}/'
    if (not os.path.exists(resultUrl)): os.makedirs(resultUrl)

    data1 = ''
    with open(f'{filepath1}.txt', 'r') as fp:
        data1 = fp.read().split('\n')

    data2 = ''
    with open(f'{filepath2}.txt', 'r') as fp:
        data2 = fp.read().split('\n')


    print(f'{filename1}拥有词汇数：{len(data1)}')
    print(f'{filename2}拥有词汇数：{len(data2)}\n')

    common1 = []
    common2 = []
    exclusive1 = []
    exclusive2 = []
    all = []

    for item in data1:
        if item in data2:
            common1.append(item)
        else:
            exclusive1.append(item)

        if (item not in all):
            all.append(item)

    for item in data2:
        if item in data1:
            common2.append(item)
        else:
            exclusive2.append(item)

        if (item not in all):
            all.append(item)

    print(f'{filename1}与{filename2}合计词汇数：{len(all)}\n'
          f'{filename1}与{filename2}共有词汇数：{len(common1)}\n'
          f'{filename2}与{filename1}共有词汇数：{len(common2)}\n'
          f'{filename1}相比与{filename2}独有词汇数：{len(exclusive1)}\n'
          f'{filename2}相比与{filename1}独有词汇数：{len(exclusive2)}\n')

    with open(f'{resultUrl}{filename1}_{filename2}all.txt', 'w+') as fp:
        for item in all:
            fp.write(item+'\n')

    with open(f'{resultUrl}{filename1} {filename2}common.txt', 'w+') as fp:
        for item in common1:
            fp.write(item+'\n')

    with open(f'{resultUrl}{filename2} {filename1}common.txt', 'w+') as fp:
        for item in common2:
            fp.write(item+'\n')

    with open(f'{resultUrl}{filename1} {filename2}exclusive.txt', 'w+') as fp:
        for item in exclusive1:
            fp.write(item+'\n')

    with open(f'{resultUrl}{filename2} {filename1}exclusive.txt', 'w+') as fp:
        for item in exclusive2:
            fp.write(item+'\n')


if __name__ == '__main__':
    main('小学词汇', '初中词汇')
    main('小学词汇_初中词汇', '高中词汇', filepath1='result')
    main('小学词汇_初中词汇_高中词汇', '四级词汇', filepath1='result')
    main('小学词汇_初中词汇_高中词汇_四级词汇', '六级词汇', filepath1='result')
    main('小学词汇_初中词汇_高中词汇_四级词汇_六级词汇', '考研词汇', filepath1='result')
    main('四级词汇', '六级词汇')
    main('六级词汇', '考研词汇')
    main('四级词汇', '考研词汇')
    main('四级词汇_六级词汇', '考研词汇', filepath1='result')
    main('小学词汇_初中词汇_高中词汇', '六级词汇_考研词汇', filepath1='result', filepath2='result')